package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.AuditMapper;
import com.hbnu.system.model.entity.Audit;
import com.hbnu.system.service.AuditService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class AuditServiceImpl extends ServiceImpl<AuditMapper, Audit> implements AuditService {

    @Override
    public boolean addAudit(Audit audit) {
        if(StrUtil.isEmpty(audit.getSynthesisID()) || StrUtil.isBlank(audit.getSynthesisID())){
            throw new BaseException("未得到相关联id!");
        }
        if(StrUtil.isEmpty(audit.getAuditerRole()) || StrUtil.isBlank(audit.getAuditerRole())){
            throw new BaseException("未得到审核员职位");
        }
        if(StrUtil.isEmpty(audit.getAuditerName()) || StrUtil.isBlank(audit.getAuditerName())){
            throw new BaseException("未得到审核员姓名");
        }
        if(StrUtil.isEmpty(audit.getStatusChange()) || StrUtil.isBlank(audit.getStatusChange())){
            throw new BaseException("未得到状态变化");
        }
        if(Objects.isNull(audit.getAuditerTime())){
            throw new BaseException("未得到审核时间");
        }
        return baseMapper.insert(audit) == 1;
    }

    @Override
    public List<Audit> selectAuidtByFRI(String id) {
        List<Audit> audits = baseMapper.selectList(Wrappers.lambdaQuery(Audit.class)
                .eq(Audit::getSynthesisID,id));
        if(Objects.isNull(audits)){
            throw new BaseException("输入的横向项目编号有误");
        }
        return audits;
    }
}
